package com.example.recuitactivity.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.recuitactivity.helper.DbOpenHelper;
import com.example.recuitactivity.EditRecuitInfoActivity;
import com.example.recuitactivity.R;
import com.example.recuitactivity.bean.RecuitInfo;

import java.util.List;

public class EnterpriseNeedInfoAdapter extends RecyclerView.Adapter<EnterpriseNeedInfoAdapter.RecuitInfoViewHolder>{
    private List<RecuitInfo> mDatas;
    private Context context;

    public EnterpriseNeedInfoAdapter(){}

    public EnterpriseNeedInfoAdapter(Context context, List<RecuitInfo> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    public void setmDatas(List<RecuitInfo> mDatas){
        this.mDatas=mDatas;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecuitInfoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.rv_recuit_info_row_item, parent, false);
        RecuitInfoViewHolder holder = new RecuitInfoViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecuitInfoViewHolder holder, int position) {
        RecuitInfo bean = mDatas.get(position);
        holder.tv_recuit_name.setText(bean.getRecuitName());
        holder.tv_salary.setText(bean.getSalary());
        holder.tv_edubg_request.setText(bean.getEduRequest());
        holder.tv_enterprise_name.setText(bean.getEnterpriseName());
        holder.tv_scale.setText(bean.getScale());
        holder.tv_location.setText(bean.getLocation());

        DbOpenHelper dbOpenHelper=new DbOpenHelper(context);
        int count=dbOpenHelper.getDeliveredNumByPid(bean.getId());
        holder.tv_delivered_number.setText(count+"人");

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, EditRecuitInfoActivity.class);
                Bundle bundle=new Bundle();
                bundle.putLong("id",bean.getId());
                bundle.putString("recruit_name",bean.getRecuitName());
                bundle.putString("salary",bean.getSalary());
                bundle.putString("edubg_request",bean.getEduRequest());
                bundle.putString("enterprise_name",bean.getEnterpriseName());
                bundle.putString("scale",bean.getScale());
                bundle.putString("location",bean.getLocation());
                bundle.putString("detail",bean.getDetail());
                intent.putExtras(bundle);
                intent.setAction("updateRecruitInfo");
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class RecuitInfoViewHolder extends RecyclerView.ViewHolder {
        TextView tv_recuit_name,tv_salary,tv_edubg_request,tv_enterprise_name,tv_scale,tv_location,tv_delivered_number;

        public RecuitInfoViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_recuit_name=(TextView) itemView.findViewById(R.id.tv_recuit_name);
            tv_salary=(TextView) itemView.findViewById(R.id.tv_salary);
            tv_edubg_request=(TextView) itemView.findViewById(R.id.tv_edubg_request);
            tv_enterprise_name=(TextView) itemView.findViewById(R.id.tv_enterprise_name);
            tv_scale=(TextView) itemView.findViewById(R.id.tv_scale);
            tv_location=(TextView) itemView.findViewById(R.id.tv_location);
            tv_delivered_number=(TextView) itemView.findViewById(R.id.tv_delivered_number);
        }
    }

}
